package com.dzl.goods.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.goods.dao.WelfareGoodsLabelRelMapper;
import com.dzl.goods.pojo.WelfareGoodsLabel;
import com.dzl.goods.pojo.WelfareGoodsLabelRel;
import com.dzl.goods.service.WelfareGoodsLabelRelService;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * (WelfareGoodsAndLabel)表服务实现类
 * @author wangjian
 * @date 2021-08-02 10:12:08
 */
@Service
public class WelfareGoodsLabelRelServiceImpl extends BaseServiceImpl<WelfareGoodsLabelRel> implements WelfareGoodsLabelRelService {

    @Autowired
    private WelfareGoodsLabelRelMapper mapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateGoodsLabelRelList(Integer labelId,String goodsIds) {
        //保存新的标签商品的关联关系
        if (EmptyUtils.isNotEmpty(goodsIds)){
            List<WelfareGoodsLabelRel> welfareGoodsLabelRels = new ArrayList<>();
            String[] split = goodsIds.split(",");
            if (EmptyUtils.isNotEmpty(split)){
                for (int i = 0; i < split.length; i++) {
                    WelfareGoodsLabelRel welfareGoodsLabelRel1 = new WelfareGoodsLabelRel();
                    welfareGoodsLabelRel1.setLabelId(labelId);
                    welfareGoodsLabelRel1.setGoodsId(Integer.parseInt(split[i]));
                    welfareGoodsLabelRels.add(welfareGoodsLabelRel1);
                }
            }
            mapper.insertList(welfareGoodsLabelRels);
        }
        return 1;
    }

    @Override
    public Integer deleteGoodsLabelRel(Integer labelId,String goodsIds) {
        int result = mapper.deleteGoodsLabelRel(labelId,goodsIds);
        return result;
    }

    @Override
    public List<WelfareGoodsLabel> selectGoodsLabelListByGoodsId(Integer goodsId,Integer labelNum) {
        return mapper.selectGoodsLabelListByGoodsId(goodsId,labelNum);
    }

}